function forEachTree(array, callback, childrenKey = 'children') {
  if (!array && !array.length) return;

  const each = (array, parent) => {
    array.forEach((item, index) => {
      callback(item, index, parent);
      if (item[childrenKey] && item[childrenKey].length) {
        each(item[childrenKey], item);
      }
    });
  };
  each(array);
}

export { forEachTree };
